package com.sgb.goods.utils.base.viewmodel;

import android.app.Application;
import android.content.res.Resources;
import android.view.View;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.sgb.goods.utils.base.GoodsSdk;
import com.sgb.goods.utils.base.LoadState;
import com.sgb.goods.utils.base.LoadStateParams;


/**
 * ViewModel基类
 * 实现了AndroidViewModel
 * @author cyj
 */
public abstract class BaseViewModel extends ViewModel implements DefaultLifecycleObserver {

    /** Resources */
    public Resources resources;
    /** Application */
    public Application application;
    /** 加载状态 */
    public MutableLiveData<LoadState> loadState = new MutableLiveData<>();

    public BaseViewModel() {
        this.application = GoodsSdk.getApplication();
    }

    /**
     * 重新加载数据。没有网络，点击重试时回调
     */
    public void reloadData() {
        // 此处没有做重新调用上一次网络请求的函数；取消提示用户无网络的覆盖视图让用户重新可以操作
    }

    /**
     * 网络加载框（无法点击其他区域取消加载框）
     */
    public void showLoading() {
        showLoading(false);
    }

    /**
     * 网络加载框
     * @param isCancelable    是否可以点击其他区域取消加载框
     */
    public void showLoading(boolean isCancelable) {
        LoadStateParams params = new LoadStateParams();
        params.isCancelable = isCancelable;
        loadState.postValue(LoadState.LOADING.loadStateParams(params));
    }

    /**
     * 取消网络加载框
     */
    public void cancelLoading() {
        loadState.postValue(LoadState.CANCEL_LOADING);
    }

    /**
     * 显示Dialog弹窗
     * @param tag
     * @param message
     */
    public void showDialog(Object tag, String message, String cancelMessage, String confirmMessage) {
        LoadStateParams params = new LoadStateParams();
        params.tag = tag;
        params.message = message + "";
        params.message1 = cancelMessage + "";
        params.message2 = confirmMessage + "";
        loadState.postValue(LoadState.DIALOG.loadStateParams(params));
    }

    /**
     * 显示Dialog弹窗
     * @param tag
     * @param message
     */
    public void showDialog(Object tag, String message) {
        LoadStateParams params = new LoadStateParams();
        params.tag = tag;
        params.message = message + "";
        loadState.postValue(LoadState.DIALOG.loadStateParams(params));
    }

    /**
     * 显示无数据UI
     */
    public void showNoData(View view) {
        if(null != view) {
            view.post(() -> {
                LoadStateParams params = new LoadStateParams();
                params.left = view.getLeft();
                params.top = 0 == view.getTop() ? ((View) view.getParent()).getTop() : view.getTop();
                params.right = view.getRight();
                params.bottom = view.getBottom();
                loadState.postValue(LoadState.NO_DATA.loadStateParams(params));
            });
        }
    }

    /**
     * 销毁activity
     */
    public void finishActivity() {
        loadState.postValue(LoadState.FINISH);
    }

    /**
     * 销毁activity 并设置 setResult()
     */
    public void finishActivity(int setResultCode) {
        LoadStateParams params = new LoadStateParams();
        params.setResultCode = setResultCode;
        loadState.postValue(LoadState.FINISH.loadStateParams(params));
    }

    /**
     * Resources
     * @return    Resources
     */
    public Resources getResources() {
        if (resources == null && null != application) {
            resources = application.getResources();
        }
        return resources;
    }
}
